package com.yx.front.manager;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yx.front.config.global.CommonResult;
import com.yx.front.converter.SysUserConverter;
import com.yx.front.enums.TimeFormat;
import com.yx.front.model.MessageRecord;
import com.yx.front.model.SysUser;
import com.yx.front.model.param.MessageRecordParam;
import com.yx.front.model.vo.MessageRecordVo;
import com.yx.front.model.vo.SysUserVo;
import com.yx.front.service.MessageRecordService;
import com.yx.front.service.SysUserService;
import com.yx.front.utils.LoginUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author YangXiong
 */
@Component
public class MessageRecordManager {
    @Resource
    private MessageRecordService messageRecordService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysUserConverter sysUserConverter;
    @Resource
    private LoginUtil loginUtil;

    public CommonResult<List<MessageRecordVo>> getMessageList(MessageRecordParam param) {
        SysUser currentUser = loginUtil.getCurrentUser();
        //查询在当前记录之后的所有记录
        LambdaQueryWrapper<MessageRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(x -> x.eq(MessageRecord::getToId, currentUser.getId()).eq(MessageRecord::getFromId, param.getFriendId())
                .or()
                .eq(MessageRecord::getToId, param.getFriendId()).eq(MessageRecord::getFromId, currentUser.getId()))
                .orderByAsc(MessageRecord::getSendTime);
        List<MessageRecord> list = messageRecordService.list(queryWrapper);
        SysUserVo currentUserVo = sysUserConverter.sysUserToVo(currentUser);
        SysUser user = sysUserService.getById(param.getFriendId());
        SysUserVo userVo = sysUserConverter.sysUserToVo(user);
        List<MessageRecordVo> result = handlerResult(list, currentUserVo, userVo);
        return CommonResult.success(result);
    }

    private List<MessageRecordVo> handlerResult(List<MessageRecord> list, SysUserVo sysUserVo, SysUserVo userVo) {
        List<MessageRecordVo> vos = new ArrayList<>();
        list.forEach(one -> {
            MessageRecordVo vo = new MessageRecordVo();
            vo.setContext(one.getContext());
            vo.setId(one.getId());
            String format = DateUtil.format(one.getSendTime(), TimeFormat.TIME_FORMAT);
            vo.setSendTime(format);
            if (one.getFromId().equals(sysUserVo.getId())) {
                vo.setFromUser(sysUserVo);
            }else {
                vo.setFromUser(userVo);
            }
            vos.add(vo);
        });
        return vos;
    }

    public CommonResult<String> send(MessageRecordParam param) {
        SysUser currentUser = loginUtil.getCurrentUser();
        MessageRecord record = new MessageRecord();
        record.setToId(param.getFriendId());
        record.setContext(param.getContext());
        record.setSendTime(DateUtil.date());
        record.setFromId(currentUser.getId());
        boolean save = messageRecordService.save(record);
        if(save){
            return CommonResult.success("已发送");
        }
        return CommonResult.fail("发送失败");
    }
}
